<template>
  <!-- 设计策划资料收集 -->
  <base-breadcrumb class="designPlanDataCollect-container">
    <searchData :searchData="searchArr" @search="handleSearch"></searchData>
    <a-card>
      <div class="table-operator">
        <a-button v-btn-permission="'ds_019_add_01'" type="primary" @click="handleUpdate('add')"
          >新增设计策划资料</a-button
        >
      </div>
      <div class="table-operateors mb20">
        <div class="pub-table-select-count">
          <a-icon type="info-circle" theme="twoTone" />
          <span>
            设计策划资料总数：
            <a>{{ calcTotal(totalInfo) || 0 }}个</a>
          </span>
          <a-divider type="vertical" />
          <span>
            待提交审核：
            <a>{{ totalInfo.draftNum || 0 }}个</a>
          </span>
          <a-divider type="vertical" />
          <span>
            审核中：
            <a>{{ totalInfo.submittedNum || 0 }}个</a>
          </span>
          <a-divider type="vertical" />
          <span>
            审核不通过：
            <a>{{ totalInfo.rejectedNum || 0 }}个</a>
          </span>
          <a-divider type="vertical" />
          <span>
            审核通过：
            <a>{{ totalInfo.pastNum || 0 }}个</a>
          </span>
        </div>
      </div>
      <list-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
        showPagination="auto"
      >
        <span slot="materialStatus" slot-scope="text, record">
          <StatusDisplay :status-code="record.materialStatus" :isApproval="true" />
        </span>
        <div slot="action" slot-scope="text, record">
          <MoreButton>
          <a v-btn-permission="'ds_019_loo_02'" @click="handleUpdate('look', record)">查看</a>
          <a
            v-btn-permission="'ds_019_edi_03'"
            v-if="record.materialStatus == 0 || record.materialStatus == 3"
            @click="handleUpdate('edit', record)"
            >编辑</a
          >
          <a
            v-btn-permission="'ds_019_del_04'"
            v-if="record.materialStatus == 0"
            @click="handleUpdate('delete', record)"
            >删除</a
          >
          <a v-if="record.materialStatus > 0" @click="handleUpdate('audit', record)">审核信息</a>
          </MoreButton>
        </div>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { typeListApproval } from '@/utils/util'
import { getList, deleteData, getTotal } from '@/api/design/deepDesign/planningDataCollect'
import StatusDisplay from '@/components/StatusDisplay'

const searchArr = [
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   valueKey: 'branchComId',
  //   type: SelectSubCompany
  // },
  // {
  //   name: '所属项目',
  //   keyValue: 'branchComId',
  //   valueKey: 'projectId',
  //   type: SelectProject
  // },
  {
    name: '设计策划资料名字',
    value: 'materialName',
    type: 'input'
  },
  {
    name: '创建时间',
    value: 'time',
    type: 'dateRange',
    attr: {
      rangeFormat: ['createStartTime', 'createEndTime']
    }
  },
  {
    name: '创建人',
    value: 'creatorName',
    type: 'input'
  },
  {
    name: '状态',
    value: 'materialStatus',
    type: 'select',
    data: typeListApproval.filter((item) => item.label !== '已作废')
  }
]
const columns = [
  {
    title: '序号',
    dataIndex: 'index',
    width: 80,
    customRender() {
      return Number(arguments[2]) + 1
    }
  },
  {
    title: '设计策划资料名称',
    ellipsis: true,
    dataIndex: 'materialName'
  },
  {
    title: '所属分公司',
    ellipsis: true,
    dataIndex: 'branchComName'
  },
  {
    title: '所属项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '项目设计管理类别',
    ellipsis: true,
    dataIndex: 'designCategoryName'
  },
  {
    title: '创建人',
    ellipsis: true,
    dataIndex: 'creatorName'
  },
  {
    title: '创建时间',
    ellipsis: true,
    dataIndex: 'createTime'
  },
  {
    title: '状态',
    ellipsis: true,
    dataIndex: 'materialStatus',
    scopedSlots: { customRender: 'materialStatus' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '180px',
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'designPlanDataCollect',
  components: {
    StatusDisplay
  },
  data() {
    return {
      searchArr: searchArr,
      columns: columns,
      totalInfo: {},
      queryParam: {}
    }
  },
  methods: {
    calcTotal(total) {
      return Object.keys(total).reduce((pre, cur) => {
        return isNaN(Number(total[cur])) ? pre : pre + Number(total[cur])
      }, 0)
    },
    async getTotalReq(params) {
      try {
        const res = await getTotal(params)
        console.log(res)
        this.totalInfo = res.data || {}
      } catch (e) {
        console.error(e)
      }
    },
    loadData(parameter) {
      const params = Object.assign({}, parameter, { param: this.queryParam })
      this.getTotalReq(this.queryParam)
      return getList(params).then((res) => {
        return {
          code:200,
          data: res.data.list || [],
          currentPage: res.data.pageNum,
          totalSize: res.data.total
        }
      })
    },
    handleSearch(val) {
      this.queryParam = val || {}
      this.$refs.table.refresh(true)
    },
    handleUpdate(actionType, data = {}) {
      const commonHandler = () => {
        this.$router.push({
          path: '/project/design/planning/designPlanCollect/update',
          query: {
            pageType: actionType,
            id: data.id || ''
          }
        })
      }
      const actionMap = {
        add: commonHandler,
        edit: commonHandler,
        look: commonHandler,
        delete: () => {
          this.$confirm({
            content: '请确认是否删除该数据？',
            onOk: async () => {
              const params = {
                id: data.id
              }
              await deleteData(params)
              this.$message.success('删除成功')
              this.handleSearch()
            }
          })
        },
        audit: () => {
          this.$bus.$emit('showApprovalDetailsDialog', data.id)
        }
      }
      actionMap[actionType] && actionMap[actionType]()
    }
  }
}
</script>

<style>
</style>
